package bupt.party.online.video;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;


public class VideoMenu {
	private static VideoMenu instance;
	public static VideoMenu getInstance(){
		if(instance == null){
			synchronized (VideoMenu.class) {
				if(instance == null){
					instance = new VideoMenu();
				}
			}
		}
		return instance;
	}
	
	private VideoMenu(){
		loadMenu();
	}
	
	public static final String WEBSITE_NAME = "bpol-1.0";
	private static final String VIDEO_MENU_FILE = "video_menu.xml";
	
	// 一级菜单
	private List<VideoMenuItem> menuList = new ArrayList<VideoMenuItem>();
	
	public List<VideoMenuItem> getMenuList() {
		return menuList;
	}

	public void loadMenu(){
		menuList.clear();
		
		String videoRootPath = System.getProperty("user.dir");
		File videoFolder = new File( videoRootPath + "/videos");
		if(!videoFolder.exists()){
			videoFolder = new File( videoRootPath + "/src/main/webapp/videos");
			if(!videoFolder.exists()){
				videoFolder = new File( videoRootPath + "/src/main/webapp/videos");
				if(!videoFolder.exists()){
					String videoHome = System.getenv("VIDEO_HOME") + "/webapps";
					videoFolder = new File(videoHome + "/videos");
					if(!videoFolder.exists()){
						videoFolder = new File(videoHome + "/" + WEBSITE_NAME + "/videos");
					}
				}
			}
		}
		videoRootPath = videoFolder.getAbsolutePath();
		File vmFile = new File(videoRootPath, VIDEO_MENU_FILE);
		if(!vmFile.exists()){
			return;
		}
		
		// Begin to read xml file
		SAXReader reader = new SAXReader();
		Document xmlDoc = null;
		try{
			xmlDoc = reader.read(vmFile);
			
		}catch (org.dom4j.DocumentException e) {
			e.printStackTrace();
		}
		
		Element rootElement = xmlDoc.getRootElement();
		Iterator elemIter = rootElement.elementIterator();
		while(elemIter.hasNext()){
			Element menuElem = (Element)elemIter.next();
			String name = menuElem.attributeValue("name");
			String path = menuElem.attributeValue("path");
			VideoMenuItem firstVMI = new VideoMenuItem();
			firstVMI.setName(name);
			firstVMI.setPath(path);
			
			Iterator innerIter = menuElem.elementIterator();
			while(innerIter.hasNext()){
				Element subMenuElem = (Element)innerIter.next();
				String subName = subMenuElem.attributeValue("name");
				String subPath = subMenuElem.attributeValue("path");
				VideoMenuItem subVMI = new VideoMenuItem();
				subVMI.setName(subName);
				subVMI.setPath(subPath);
				firstVMI.getChildren().add(subVMI);
			}
			
			menuList.add(firstVMI);
		}
	}
	
	public String getMenuStr(){
		if(menuList == null || menuList.size() == 0){
			return null;
		}
		
		StringBuffer buffer = new StringBuffer();
		Iterator<VideoMenuItem> iter = menuList.iterator();
		while(iter.hasNext()){
			VideoMenuItem item = iter.next();
			buffer.append("#");
			buffer.append(item.getPath());
			buffer.append(",");
			buffer.append(item.getName());
			if(item.getChildren().size() == 0){
				continue;
			}
			buffer.append("&");
			Iterator<VideoMenuItem> subIter = item.getChildren().iterator();
			while(subIter.hasNext()){
				VideoMenuItem subItem = subIter.next();
				buffer.append(subItem.getPath());
				buffer.append(",");
				buffer.append(subItem.getName());
				buffer.append(";");
			}
		}
		return buffer.toString();
	}
	
	public String convertCategory(String categoryId){
		if(!categoryId.startsWith("/")){
			return "Invalid categoryId";
		}
		
		VideoMenuItem item = null;
		
		categoryId = categoryId.substring(1);
		String[] categoryIdArray = categoryId.split("/");
		StringBuffer buffer = new StringBuffer();
		buffer.append("/");
		boolean flag = false;
		for(int i = 0; i < categoryIdArray.length; i++){
			String strItem = categoryIdArray[0];
			Iterator<VideoMenuItem> iter = menuList.iterator();
			while(iter.hasNext()){
				item = iter.next();
				if(item.getPath().equals(strItem)){
					flag = true;
					break;
				}
			}
			if(flag == false){
				buffer.delete(0, buffer.length());
				buffer.append("/");
				continue;
			}else{
				flag = false;
			}
			
			buffer.append(item.getName());
			buffer.append("/");
			strItem = categoryIdArray[1];
			iter = item.getChildren().iterator();
			while(iter.hasNext()){
				item = iter.next();
				if(item.getPath().equals(strItem)){
					flag = true;
					break;
				}
			}
			if(flag == false){
				buffer.delete(0, buffer.length());
				buffer.append("/");
				continue;
			}else{
				flag = false;
			}
			
			buffer.append(item.getName());
			break;
		}
		return buffer.toString();
	}
	
	public static void main(String[] args) {
		VideoMenu vm = VideoMenu.getInstance();
		String paths = "/byjy/bydk1";
		String paths1 = "/sdjy/bydk2";
	}
}
